package com.struts.deal.corpor.manage.infomanage;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.Constants;
import com.ibatis.po.corpor.TApplyBooth;
import com.ibatis.po.corpor.TCorporation;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.Common;
import com.lg.system.MessageBean;
import com.struts.web.corpor.manage.infomanage.ApplyBoothForm;
import com.struts.web.corpor.manage.infomanage.ViewApplyBoothForm;

public class ApplyBoothDeal {

	public static void viewApplyBooth(HttpServletRequest request,
			HttpServletResponse response,ViewApplyBoothForm aWebForm){
		CTurnPage page = new CTurnPage();
		TCorporation corpor  = (TCorporation)request.getSession().getAttribute("Corporation");
		try{

			if(corpor!=null){
				String corporId = corpor.getId();
				page.setQueryList("T_APPLY_BOOTH.selectApplyBooth",corporId);
				page.setPerPageRows(10);//设置每页行数
				page.excute(request);
				request.setAttribute("page", page);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	/**
	 * 加载申请展位默认信息
	 * @param request
	 * @param response
	 * @param aWebForm
	 */
	public static MessageBean defaultApplyBooth(HttpServletRequest request,
			HttpServletResponse response,ApplyBoothForm aWebForm){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		MessageBean messageBean = new MessageBean();
		List applyDateList = null;
		int count=0;
		TCorporation corpor  = (TCorporation)request.getSession().getAttribute("Corporation");
		try{
			
			if(corpor!=null){
				//是否被列入黑名单
				count = (Integer)sqlmap.queryForObject("T_CORPORATION.selectBlackListByID",corpor);
				if(count>0){
					messageBean.setMessage(Constants.BLACKLIST);
					return messageBean;
				}
				
				//是否被批准
				count = (Integer)sqlmap.queryForObject("T_CORPORATION.selectIsApproveByID",corpor);
				if(count>0){
					messageBean.setMessage(Constants.NOT_APPROVE);
					return messageBean;
				}
				
				
					applyDateList = sqlmap.queryForList("T_ACTIVITY.selectApplyDateList");

					request.getSession().setAttribute("applyDateList", applyDateList);
					messageBean.setResult(true);
					
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
	/**
	 * 加载修改的信息
	 * @param request
	 * @param response
	 * @param aWebForm
	 */
	public static MessageBean loadApplyBooth(HttpServletRequest request,
			HttpServletResponse response,ApplyBoothForm aWebForm){
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String applyId = request.getParameter("applyId");
		TApplyBooth applyBoothPo = new TApplyBooth();
		List applyDateList = null;
		int count=0;
		MessageBean messageBean = new MessageBean();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		TCorporation corpor  = (TCorporation)request.getSession().getAttribute("Corporation");
		try{
			applyBoothPo.setId(applyId);
			applyBoothPo = (TApplyBooth)sqlmap.queryForObject("T_APPLY_BOOTH.selectById",applyBoothPo);
            if(applyBoothPo!=null){
            	aWebForm.setApplyDate(df.format(applyBoothPo.getAppDate()));
            	aWebForm.setTimeType(applyBoothPo.getAppTime());
            	aWebForm.setRecruitContent(applyBoothPo.getRecruitContent());
            }
            
            if(corpor!=null){
				//是否被列入黑名单
				count = (Integer)sqlmap.queryForObject("T_CORPORATION.selectBlackListByID",corpor);
				if(count>0){
					messageBean.setMessage(Constants.BLACKLIST);
					return messageBean;
				}
				
				//是否被批准
				count = (Integer)sqlmap.queryForObject("T_CORPORATION.selectIsApproveByID",corpor);
				if(count>0){
					messageBean.setMessage(Constants.NOT_APPROVE);
					return messageBean;
				}
				
				
					applyDateList = sqlmap.queryForList("T_ACTIVITY.selectApplyDateList");

					request.getSession().setAttribute("applyDateList", applyDateList);
					messageBean.setResult(true);
					
			}
			request.setAttribute("applyId", applyId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 申请展位
	 * @param request
	 * @param response
	 * @param aWebForm
	 */
	public static MessageBean applyBooth(HttpServletRequest request,
			HttpServletResponse response,ApplyBoothForm aWebForm) throws Exception{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		MessageBean messageBean = new MessageBean();
		TCorporation corpor  = (TCorporation)request.getSession().getAttribute("Corporation");
		TApplyBooth applyBoothPo = new TApplyBooth();
		int count=0;
		DateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dappDate = ft.parse(aWebForm.getApplyDate());
		Timestamp appDate = new java.sql.Timestamp(dappDate.getTime());
		try{
			if(corpor!=null){
				count = (Integer)sqlmap.queryForObject("T_CORPORATION.selectBlackListByID",corpor);
				if(count<1){
					applyBoothPo.setId(Common.getNewID());
					applyBoothPo.setAppDate(appDate);
					applyBoothPo.setAppTime(aWebForm.getTimeType());
					
					String recruitContent = aWebForm.getRecruitContent();
					recruitContent=recruitContent.replace("<p>", "");
					recruitContent=recruitContent.replace("</p>", "<br>");
					applyBoothPo.setRecruitContent(recruitContent);
					applyBoothPo.setCorporId(corpor.getId());
					sqlmap.insert("T_APPLY_BOOTH.applyBooth",applyBoothPo);
					messageBean.setResult(true);
				}else{
					messageBean.setMessage(Constants.NOTEXIST_OR_DEL);
				}
				
			}
		}catch(Exception e){
			messageBean.setMessage(Constants.APPLY_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 * 修改招聘信息
	 * @param request
	 * @param response
	 * @param aWebForm
	 */
	public static MessageBean editRecruitContent(HttpServletRequest request,
			HttpServletResponse response,ApplyBoothForm aWebForm) throws SQLException{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String applyId = request.getParameter("applyId");
		TApplyBooth applyBoothPo = new TApplyBooth();
		MessageBean messageBean = new MessageBean();
		int count = 0;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try{
			applyBoothPo.setId(applyId);
			if(aWebForm.getApplyDate()!=null && aWebForm.getApplyDate()!=""){
				java.util.Date dappDate = df.parse(aWebForm.getApplyDate());
				java.sql.Timestamp appDate = new java.sql.Timestamp(dappDate.getTime());
				applyBoothPo.setAppDate(appDate);
			}
			
			applyBoothPo.setRecruitContent(aWebForm.getRecruitContent());
			System.out.println("find:"+applyId);
			System.out.println("find:"+count);
			//检查是否和其它重复
			count=((Integer)sqlmap.queryForObject("T_APPLY_BOOTH.selectIsApproveAndEqualOthers",applyBoothPo)).intValue();
			if(count>0){
				messageBean.setMessage(Constants.EQUAL_OTHERS);
				return messageBean;
			}
			//检查是否已被批准
         count=((Integer)sqlmap.queryForObject("T_APPLY_BOOTH.selectIsApprove",applyBoothPo)).intValue();
			System.out.println("find:"+count);
			if(count<1){
				sqlmap.update("T_APPLY_BOOTH.updateRecruitContent",applyBoothPo);
			messageBean.setResult(true);
			}else{
				messageBean.setResult(false);
				messageBean.setMessage(Constants.CONFIRM_NOT_EDIT);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return messageBean;
	}
}
